import _ from 'lodash';
export const ECHO_TYPE = {
  secMenu: {
    id: 'mappingMenuId',
    name: 'secondMenuName',
    appName: 'applicationName',
  },
  role: {
    id: 'roleId',
    name: 'roleName',
    appName: '',
  },
};
export const operation_Type = {
  add: 1,
  edit: 2,
  delete: 3,
};

// 获取checkbox选中的id
export const get_checkbox_ids = (arr) => {
  const _ids = _.reduce(
    arr,
    (_t, _v) => {
      if (_v.operationType !== 3) {
        return [..._t, _v.id];
      } else {
        return _t;
      }
    },
    [],
  );
  return _ids;
};

// 判断删除的菜单是否是默认的菜单
export const delete_menu = ({ arr = [], value = '' }) => {
  const _menu = _.reduce(
    arr,
    (t, _v) => {
      if (_v.id === value) {
        if (_v.secondMenuId) {
          _v.operationType = operation_Type['delete'];
          _v.homePageFlag = false;
          return [...t, _v];
        } else {
          return t;
        }
      } else {
        return [...t, _v];
      }
    },
    [],
  );
  return _menu;
};

/**
 * 批量删除剩下的id，不能删除 saasAdminFlag：true字段
 * @param {*} arr
 */
export const delete_all_checkded_id = (arr) => {
  return _.reduce(
    arr,
    (t, v) => {
      if (v.saasAdminFlag) {
        return t;
      } else {
        return [...t, v.id];
      }
    },
    [],
  );
};

/**
 * 是否选中- 不是全选
 * @param {*} ids 选中的id
 * @param {*} checkList 显示的id
 * @returns
 */
export const checkbox_is_checked = (ids = [], checkList = []) => {
  let indeterminate = false,
    checkAll = false;
  if (!_.isArray(ids) && !_.isArray(checkList)) {
    return { indeterminate, checkAll };
  }
  if (ids.length === checkList.length && checkList.length > 0) {
    indeterminate = false;
    checkAll = true;
  } else if (ids.length < checkList.length && ids.length > 0) {
    indeterminate = true;
    checkAll = false;
  } else if (ids.length === 0) {
    indeterminate = false;
    checkAll = false;
  }
  return { indeterminate, checkAll };
};

/**
 * 批量删除 菜单
 * @param {*} menuArr 当前的菜单
 * @param {*} ids 要删除的id
 * @returns
 */
export const batch_delete_menu = (menuArr = [], ids = []) => {
  return menuArr.reduce((t, v) => {
    if (ids.includes(v.id) && v.secondMenuId) {
      v.operationType = operation_Type['delete'];
      v.homePageFlag = false;
      return [...t, v];
    } else if (ids.includes(v.id) && !v.secondMenuId) {
      return t;
    } else {
      return [...t, v];
    }
  }, []);
};

/**
 * 数组元素交换位置
 * @param {array} arr 数组
 * @param {number} hoverId 添加项目的位置
 * @param {number} dragId 删除项目的位置
 * index1和index2分别是两个数组的索引值，即是两个要交换元素位置的索引值，如1，5就是数组中下标为1和5的两个元素交换位置
 */
export const swapArr = (arr, hoverId, dragId) => {
  const array = [...(arr || [])];
  const index1 = _.findIndex(array, (o) => o.id === dragId);
  const index2 = _.findIndex(array, (o) => o.id === hoverId);
  return { index1, index2 };
};
